package com.taint.asm.service;

import com.taint.asm.core.DataFlowClassVisitor;
import com.taint.asm.model.ClassReference;
import com.taint.asm.model.InheritanceMap;
import com.taint.asm.model.MethodReference;
import org.objectweb.asm.ClassReader;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DataFlowService {


    public static void start(InheritanceMap inheritanceMap, List<MethodReference.Handle> sortedMethods, Map<String, String> classFileByName, Map<ClassReference.Handle, ClassReference> classMap, Map<MethodReference.Handle, Set<Integer>> dataFlow) {

        for(MethodReference.Handle method:sortedMethods){

            if(method.getName().equals("<clinit>")){
                continue;
            }
            String file = classFileByName.get(method.getClassReference().getName());
            try {
                InputStream ins = new FileInputStream(file);
                ClassReader cr = new ClassReader(ins);
                ins.close();
                DataFlowClassVisitor cv = new DataFlowClassVisitor(classMap, inheritanceMap, dataFlow, method);
                cr.accept(cv, ClassReader.EXPAND_FRAMES);
                dataFlow.put(method, cv.getReturnTaint());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }
}
